<?php
require_once 'ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/quick_message.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/message.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/avatar.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busQuick_Message.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busMessage.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAvatar.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/stringUtil.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/simpleImage.php';

class SendController extends AbstractController {
	private $_quickMessageBus;
	private $_messageBus;
	private $_avatarBus;
	private $_resourceBus;
	
	public function main(array $getVars) {
		global $ubi;
		
		$this->_quickMessageBus = new Quick_MessageBus();
		$this->_messageBus = new MessageBus();
		$this->_avatarBus = new AvatarBus();
		$this->_resourceBus = new ResourceBus();
		
		$userID = $_SESSION['userid'];
		
		if ($getVars['type'] == 'quick_message') {
			if ($userID) {				
				$quick_message = new Quick_MessageModel();
				$quick_message->setContent($_POST['content']);
				$quick_message->setReceiverId($getVars['userid']);
				$quick_message->setSenderId($userID);
				
				echo $this->_quickMessageBus->addQuick_Message($quick_message);
			}		
		} else if ($getVars['type'] == 'message') {
			if ($userID) {
				$mess = new MessageModel();
				$mess->setContent($_POST['mess']);
				$mess->setReceiverId($_POST['receiverId']);
				$mess->setSenderId($userID);
				$mess->setStatus($ubi['MESSAGE']['STATUS_CASUAL']);
				$mess->setTitle($_POST['title']);
				echo $this->_messageBus->addMessage($mess);
			}
		} else if ($getVars['type'] == 'avatar') {
			if ($userID) {
				$fileName = $_FILES['avatar']['name'];
				if (file_exists($ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName)) { 
					$fileName = StringUtil::changeFileName($fileName);
				}
				 
				//create resource model object			
				$res = new ResourceModel();
				$res->setResourceName($fileName);
				$res->setShortDescription('avatar');
				
				$id = $this->_resourceBus->addResource($res);
				
				if ($id != 0) {
					$fileSavedResourceName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName;
					$fileSavedThumbnailName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['THUMBNAIL_PATH'] . '/' . $fileName;
					if (move_uploaded_file($_FILES['avatar']['tmp_name'], $fileSavedResourceName)) {
						copy($fileSavedResourceName, $fileSavedThumbnailName);
						
						$image = new SimpleImage();
						$image->load($fileSavedThumbnailName);
						$image->resize($ubi['SITE']['WIDTH_THUMBNAIL'], $ubi['SITE']['HEIGHT_THUMBNAIL']);
						$image->save($fileSavedThumbnailName);

						$image->load($fileSavedResourceName);
						$image->resize($ubi['SITE']['WIDTH_RESOURCE'], $ubi['SITE']['HEIGHT_RESOURCE']);
						$image->save($fileSavedResourceName);
						
						$avatar = new AvatarModel();
						$avatar->setResourceID($id);
						$avatar->setUserID($userID);
						
						echo $this->_avatarBus->addAvatar($avatar);
					}
				}
			}
		} else if ($getVars['type'] == 'defaultAvatar') {
			if ($userID) {
				$id = $_POST['id'];
				if ($id) {
					echo $this->_avatarBus->setDefaultAvatar($userID, $id);
				}
			}
		} else if ($getVars['type'] == 'deleteAvatar') {
		if ($userID) {
				$id = $_POST['id'];
				if ($id) {
					echo $this->_avatarBus->deleteAvatar($id);
				}
			}
		}		 
	}
}
?>